package com.ttn.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ttn.business.service.CourseUnitGrammarTestService;
import com.ttn.common.core.entity.PageQuery;
import com.ttn.common.core.entity.Result;
import com.ttn.common.core.entity.business.CourseUnit;
import com.ttn.common.core.entity.business.CourseUnitGrammarTest;
import com.ttn.common.core.entity.business.bo.ModifyCourseUnitGrammarTestSortBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * (course_unit_grammar_test)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/course-unit-grammar-test")
@Slf4j
public class CourseUnitGrammarTestController {
    /**
     * 服务对象
     */
    @Resource
    private CourseUnitGrammarTestService courseUnitGrammarTestService;

    /**
     * 保存语法测试
     *
     * @param request
     * @param courseUnitGrammarTest
     * @return
     */
    @PostMapping("/save/course-unit-grammar-test")
    public Result saveCourseUnitGrammarTest(HttpServletRequest request, @RequestBody CourseUnitGrammarTest courseUnitGrammarTest) {
        log.info("CourseUnitGrammarTestController saveCourseUnitGrammarTest ,参数:{}", courseUnitGrammarTest);
        LambdaQueryWrapper<CourseUnitGrammarTest> courseUnitGrammarTestLambdaQueryWrapper = new LambdaQueryWrapper();
        courseUnitGrammarTestLambdaQueryWrapper.eq(CourseUnitGrammarTest::getCourseUnitId, courseUnitGrammarTest.getCourseUnitId());
        courseUnitGrammarTest.setSort(Math.toIntExact(courseUnitGrammarTestService.count(courseUnitGrammarTestLambdaQueryWrapper)) + 1);
        courseUnitGrammarTestService.save(courseUnitGrammarTest);
        return Result.success();
    }

    /**
     * 修改语法测试
     *
     * @param request
     * @param courseUnitGrammarTest
     * @return
     */
    @PutMapping("/modify/course-unit-grammar-test")
    public Result modifyCourseUnitGrammarTest(HttpServletRequest request, @RequestBody CourseUnitGrammarTest courseUnitGrammarTest) {
        log.info("CourseUnitGrammarTestController modifyCourseUnitGrammarTest ,参数:{}", courseUnitGrammarTest);
        courseUnitGrammarTestService.updateById(courseUnitGrammarTest);
        return Result.success();
    }

    /**
     * 通过单元id查询语法测试
     *
     * @param request
     * @param courseUnitId
     * @param pageQuery
     * @return
     */

    @PostMapping("/query/course-unit-grammar-test-by-course-unit-id-page/{courseUnitId}")
    public Result queryCourseUnitGrammarTestByCourseUnitIdPage(HttpServletRequest request, @PathVariable("courseUnitId") Integer courseUnitId, @RequestBody PageQuery pageQuery) {
        log.info("CourseUnitGrammarTestController queryCourseUnitGrammarStudyByCourseUnitIdPage ,参数courseUnitId:{},pageQuery:{}", courseUnitId, pageQuery);
        IPage<CourseUnitGrammarTest> courseUnitGrammarStudyIPage = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        LambdaQueryWrapper<CourseUnitGrammarTest> courseUnitGrammarStudyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseUnitGrammarStudyLambdaQueryWrapper.eq(CourseUnitGrammarTest::getCourseUnitId, courseUnitId);
        courseUnitGrammarStudyLambdaQueryWrapper.orderByAsc(CourseUnitGrammarTest::getSort);
        return Result.success(courseUnitGrammarTestService.page(courseUnitGrammarStudyIPage, courseUnitGrammarStudyLambdaQueryWrapper));
    }

    /**
     * 查看单元题目数
     *
     * @param request
     * @param courseUnitId
     * @return
     */
    @PostMapping("/service/query/course-unit-grammar-test-by-course-unit-id/{courseUnitId}")
    public Result serviceQueryCourseUnitGrammarTestByCourseUnitId( @PathVariable("courseUnitId") Integer courseUnitId) {
        log.info("CourseUnitGrammarTestController serviceQueryCourseUnitGrammarTestByCourseUnitId ,参数courseUnitId:{}", courseUnitId);
        LambdaQueryWrapper<CourseUnitGrammarTest> courseUnitGrammarStudyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseUnitGrammarStudyLambdaQueryWrapper.eq(CourseUnitGrammarTest::getCourseUnitId, courseUnitId);
        return Result.success(courseUnitGrammarTestService.count(courseUnitGrammarStudyLambdaQueryWrapper));
    }

    /**
     * 修改语法试题排序
     *
     * @param request
     * @param modifyCourseUnitGrammarTestSortBO
     * @return
     */
    @PutMapping("/modify/course-unit-grammar-test-sort")
    public Result modifyCourseUnitGrammarTestSort(HttpServletRequest request, @RequestBody ModifyCourseUnitGrammarTestSortBO modifyCourseUnitGrammarTestSortBO) {
        log.info("CourseUnitGrammarTestController modifyCourseUnitGrammarTestSort ,参数:{}", modifyCourseUnitGrammarTestSortBO);
        CourseUnitGrammarTest courseUnitGrammarTest = courseUnitGrammarTestService.getById(modifyCourseUnitGrammarTestSortBO.getId());
        LambdaQueryWrapper<CourseUnitGrammarTest> courseUnitGrammarTestLambdaQueryWrapper = new LambdaQueryWrapper();
        courseUnitGrammarTestLambdaQueryWrapper.ne(CourseUnitGrammarTest::getId, modifyCourseUnitGrammarTestSortBO.getId());
        courseUnitGrammarTestLambdaQueryWrapper.eq(CourseUnitGrammarTest::getCourseUnitId, courseUnitGrammarTest.getCourseUnitId());
        courseUnitGrammarTestLambdaQueryWrapper.orderByAsc(CourseUnitGrammarTest::getSort);
        List<CourseUnitGrammarTest> courseUnitGrammarTestList = courseUnitGrammarTestService.list(courseUnitGrammarTestLambdaQueryWrapper);
        if (courseUnitGrammarTestList.size() > 0) {
            int sortIndex = 0;
            if (modifyCourseUnitGrammarTestSortBO.getSortingBehavior().equals(-1)) {
                if (courseUnitGrammarTest.getSort() - 2 > 0) {
                    sortIndex = courseUnitGrammarTest.getSort() - 2;
                }
            } else if (modifyCourseUnitGrammarTestSortBO.getSortingBehavior().equals(1)) {
                sortIndex = courseUnitGrammarTest.getSort();
            }
            courseUnitGrammarTestList.add(sortIndex, courseUnitGrammarTest);
            AtomicInteger i = new AtomicInteger(1);
            courseUnitGrammarTestList.stream().forEach(courseUnitGrammarTest1 -> {
                courseUnitGrammarTest1.setSort(i.getAndIncrement());
            });
            courseUnitGrammarTestService.updateBatchById(courseUnitGrammarTestList);
        }

        return Result.success();
    }

    /**
     * 删除单个语法
     *
     * @param request
     * @param id
     * @return
     */
    @DeleteMapping("/delete/course-unit-grammar-test-by-id/{id}")
    public Result deleteCourseUnitGrammarTestById(HttpServletRequest request, @PathVariable("id") Integer id) {
        log.info("CourseUnitGrammarTestController deleteCourseUnitGrammarTestById ,参数id:{}", id);
        CourseUnitGrammarTest courseUnitGrammarTest = courseUnitGrammarTestService.getById(id);
        courseUnitGrammarTestService.removeById(id);
        LambdaQueryWrapper<CourseUnitGrammarTest> courseUnitGrammarTestLambdaQueryWrapper = new LambdaQueryWrapper();
        courseUnitGrammarTestLambdaQueryWrapper.ne(CourseUnitGrammarTest::getId, id);
        courseUnitGrammarTestLambdaQueryWrapper.eq(CourseUnitGrammarTest::getCourseUnitId, courseUnitGrammarTest.getCourseUnitId());
        courseUnitGrammarTestLambdaQueryWrapper.orderByAsc(CourseUnitGrammarTest::getSort);
        List<CourseUnitGrammarTest> courseUnitGrammarTestList = courseUnitGrammarTestService.list(courseUnitGrammarTestLambdaQueryWrapper);
        if (courseUnitGrammarTestList.size() > 0) {
            AtomicInteger i = new AtomicInteger(1);
            courseUnitGrammarTestList.stream().forEach(courseUnitGrammarTest1 -> {
                courseUnitGrammarTest1.setSort(i.getAndIncrement());
            });
            courseUnitGrammarTestService.updateBatchById(courseUnitGrammarTestList);
        }
        return Result.success();
    }


}
